package com.dhcc.bpm.common.segmentId;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @description  todo
 * @author guogang
 * @date 2020年7月18日 下午3:04:24
 * @version 1.0.0 
 * @version 1.0.1  修改nowDate为newDate  添加字段流水长度len
 */
public class SegmentId {
	private long maxId;
	private long Id;

	private String bizType;
	private AtomicLong currentId;
	private long loadingId;
	private long step;

	private long version;
	private String curDate;
	private String newDate;
	private long idLimit;
	private long len;  // 添加分段id的长度
	
	public Result nextId() {
		long id = currentId.addAndGet(1);
		if (id > maxId) {
			return new Result(ResultCode.OVER, id);
		}
		if (id >= loadingId) {
			return new Result(ResultCode.LOADING, id);
		}
		return new Result(ResultCode.NORMAL, id);
	}

	/**
	 * 是否能用
	 * 
	 * @return
	 */
	public boolean isUsed() {
		return currentId.get() <= maxId;
	}

	public long getMaxId() {
		return maxId;
	}

	public void setMaxId(long maxId) {
		this.maxId = maxId;
	}

	public long getId() {
		return Id;
	}

	public void setId(long id) {
		Id = id;
	}

	public AtomicLong getCurrentId() {
		return currentId;
	}

	public void setCurrentId(AtomicLong currentId) {
		this.currentId = currentId;
	}

	public long getStep() {
		return step;
	}

	public void setStep(long step) {
		this.step = step;
	}

	public long getVersion() {
		return version;
	}

	public void setVersion(long version) {
		this.version = version;
	}

	public String getBizType() {
		return bizType;
	}

	public void setBizType(String bizType) {
		this.bizType = bizType;
	}

	public long getLoadingId() {
		return loadingId;
	}

	public void setLoadingId(long loadingId) {
		this.loadingId = loadingId;
	}

	public String getCurDate() {
		return curDate;
	}

	public void setCurDate(String curDate) {
		this.curDate = curDate;
	}



	public long getIdLimit() {
		return idLimit;
	}

	public void setIdLimit(long idLimit) {
		this.idLimit = idLimit;
	}

	@Override
	public String toString() {
		return "SegmentId [maxId=" + maxId + ", Id=" + Id + ", bizType=" + bizType + ", currentId=" + currentId
				+ ", loadingId=" + loadingId + ", step=" + step + ", version=" + version + ", curDate=" + curDate
				+ ", newDate=" + newDate + ", idLimit=" + idLimit + ", len=" + len + "]";
	}

	public long getLen() {
		return len;
	}

	public void setLen(long len) {
		this.len = len;
	}

	public String getNewDate() {
		return newDate;
	}

	public void setNewDate(String newDate) {
		this.newDate = newDate;
	}
}
